//
//  ImagePickerTool.m
//  GuiBao
//
//  Created by 文彬 on 2017/2/17.
//  Copyright © 2017年 文彬. All rights reserved.
//

#import "ImagePickerTool.h"

static ImagePickerTool *imagePicker;

@interface ImagePickerTool()
{
    BOOL isEdit;
}

@end
@implementation ImagePickerTool

+ (ImagePickerTool*)sharePicker
{
    @synchronized (self) {
        if (imagePicker==nil)
        {
            imagePicker = [[ImagePickerTool alloc]init];
        }
    }
    
    return imagePicker;
}

- (void)showImagePicker:(UIViewController*)controller
              allowEdit:(BOOL)eidt
               complete:(CompleteHandle)handle
{
    self.controller = controller;
    self.completeHandle = handle;
    isEdit = eidt;
    
    [UIAlertController showActionSheetInViewController:self.controller
                                             withTitle:nil
                                               message:@"选择照片"
                                     cancelButtonTitle:@"取消"
                                destructiveButtonTitle:nil
                                     otherButtonTitles:@[@"从相册上传",@"拍照上传"] popoverPresentationControllerBlock:^(UIPopoverPresentationController * _Nonnull popover) {
                                         
                                     } tapBlock:^(UIAlertController * _Nonnull controller, UIAlertAction * _Nonnull action, NSInteger buttonIndex) {
                                         
                                         if (buttonIndex==0) {
                                             return ;
                                         }
                                         
                                         UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
                                         imagePickerController.delegate = self;
                                         imagePickerController.allowsEditing = eidt;
                                         if (buttonIndex==2) //从相册选择
                                         {
                                             imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                                         }
                                         else if(buttonIndex==3) //拍照上传
                                         {
                                             imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                                         }
                                         [self.controller presentViewController:imagePickerController animated:YES completion:nil];
                                     }];
}

#pragma mark - image picker delegte
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    @autoreleasepool
    {
        UIImage *image ;
        if (isEdit) {
            image = [info objectForKey:UIImagePickerControllerEditedImage];
        }
        else
        {
            image = [info objectForKey:UIImagePickerControllerOriginalImage];
        }
        
        if (self.completeHandle) {
            self.completeHandle(image);
        }
        
        [picker dismissViewControllerAnimated:YES completion:^{}];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [ self.controller dismissViewControllerAnimated:YES completion:^{}];
}


@end
